<?php
namespace app\common\logic;
use think\facade\Db;

//账户充值和消费扣款
class MemberAccountLogic{

	public static $init;
	static function init(){
		if (!isset(self::$init)) {//判断是否以前创建了当前类的实例
            $c = __CLASS__;//获取类名
            self::$init = new static;//如果没有创建，实例化当前类，这里实现类只实例化一次
        }
        return self::$init;//返回类的实例
	}


	//账户金额充值(进账)
	function accountIn($data){
		$amount = $data['recharge_amount']+$data['level_amount']+$data['gift_amount']+$data['first_amount'];
		$data['amount'] = $amount;
		$data['charge_type'] = 100;  //充值
		$data['charge_io'] = 0;
		$data['operator'] = '系统';
		$data['remark'] = '充值';
		$data['czje'] = $data['recharge_amount'];
		//增加主账户金额
		$total_amount = $this->inUpdateMainAccount($data);
		//在添加金额之前的账户金额
		$data['current_amount'] = $total_amount;
		//新增主账户日志
		$log_id = $this->inAddMainAccountLog($data);
		if(!$log_id){
			return 0;
		}
		$data['member_account_log_id'] = $log_id;
		//子账户操作
		//账户充值
		if($data['recharge_amount']>0){
			$data['amount'] = $data['recharge_amount'];
			$data['charge_type'] = 100;
			$rs = $this->inAddSubAccount($data);
			if(!$rs){
				return 0;
			}
		}
		//等级返现
		if($data['level_amount']>0){
			$data['amount'] = $data['level_amount'];
			$data['charge_type'] = 105;
			$data['remark'] = '会员等级返点';
			$rs = $this->inAddSubAccount($data);
			if(!$rs){
				return 0;
			}
		}
		//活动返现
		if($data['gift_amount']>0){
			$data['amount'] = $data['gift_amount'];
			$data['charge_type'] = 110;
			$data['remark'] = '活动返点';
			$rs = $this->inAddSubAccount($data);
			if(!$rs){
				return 0;
			}
		}
		//首充返现
		if($data['first_amount']>0){
			$data['amount'] = $data['first_amount'];
			$data['charge_type'] = 120;
			$data['remark'] = '首单充值返点';
			$rs = $this->inAddSubAccount($data);
			if(!$rs){
				return 0;
			}
		}
		//邀请人返现
		if($data['origin_invitation_amount']>0){
			$data2 = $data;
			$data2['charge_type'] = 115;
			$data2['amount'] = $data['origin_invitation_amount'];
			$data2['member_id'] = $data['origin_member_id'];
			$data2['remark'] = '被邀请人充值返点';
			$data2['czje'] = 0;
			$amount2 = $this->inUpdateMainAccount($data2);
			$data2['current_amount'] = $amount2;
			
			//新增主账户日志
			$log_id2 = $this->inAddMainAccountLog($data2);
			$data2['member_account_log_id'] = $log_id2;
			$rs = $this->inAddSubAccount($data2);
			if(!$rs || !$amount2 || !$log_id2){
				return 0;
			}
		}
		//邀请人账户操作
		//最终返回主账户日志id
		return $log_id;
	}
	function inUpdateMainAccount($data){
		$info = Db::name('member_account')
				->where('member_id', $data['member_id'])
				->find();
		if(!$info){
			return 0;
		}

		//充值金额+等级返现金额+活动发现金额+首充金额+账户当前总金额
		$total_amount = $data['amount']+$info['total_amount'];
		$recharge_total_amount = $data['czje']+$info['recharge_total_amount'];
		if(!Db::name('member_account')->where('member_id', $data['member_id'])->update(['total_amount'=>$total_amount,'recharge_total_amount'=>$recharge_total_amount])){
			return 0;
		}
		//返回当前总金额
		return $total_amount;
	}
	function inAddMainAccountLog($data){
		$upd = [];
		$upd['member_id'] = $data['member_id'];
		$upd['operate_module_id'] = $data['id'];
		$upd['amount'] = $data['amount'];
		$upd['current_amount'] = $data['current_amount'];
		$upd['charge_io'] = $data['charge_io'];
		$upd['charge_type'] = $data['charge_type'];
		$upd['operator'] = $data['operator'];
		$upd['remark'] = $data['remark'];
		$upd['create_time'] = date('Y-m-d H:i:s');
		$id = Db::name('member_account_log')->insertGetId($upd);
		if(!$id){
			return 0;
		}else{
			return $id;
		}
	}
	function inAddSubAccount($data){
		$addData = [];
		$addData['member_id'] = $data['member_id'];
		//充值金额
		$addData['amount_type'] = $data['charge_type'];	//充值
		$addData['init_amount'] = $data['amount'];
		$addData['current_amount'] = $data['amount'];
		$addData['create_time'] = date('Y-m-d H:i:s');
		$member_account_sub_id = Db::name('member_account_sub')->insertGetId($addData);
		if(!$member_account_sub_id){
			return 0;
		}
		$data['member_account_sub_id'] = $member_account_sub_id;
		//添加子账户日志
		$rs = $this->inAddSubAccountLog($data,);
		if(!$rs){
			return 0;
		}
		return 1;
	}
	function inAddSubAccountLog($data){
		$upd = [];
		$upd['member_id'] = $data['member_id'];
		$upd['member_account_sub_id'] = $data['member_account_sub_id'];
		$upd['member_account_log_id'] = $data['member_account_log_id'];
		$upd['operate_module_id'] = $data['id'];
		$upd['amount'] = $data['amount'];
		$upd['current_amount'] = $data['amount'];
		$upd['charge_io'] = $data['charge_io'];
		$upd['charge_type'] = $data['charge_type'];
		$upd['remark'] = $data['remark'];
		$upd['create_time'] = date('Y-m-d H:i:s');

		$rs = Db::name('member_account_sub_log')->insert($upd);
		if(!$rs){
			return 0;
		}else{
			return 1;
		}
	}
	//--------------------------------上面的OK了--------------------------------------

	//订单扣款
	function accountOut($order_sn='', $type=1){
		if(!$order_sn){
			return 0;
		}
		$info = "";
		$data = [];
		if($type==2){
			//邮箱订单支付
			$info = Db::name('order_email')->where('email_sn', $order_sn)->find();
			if($info['status']==20){
				return 1;
			}
			if($info['status']!=10){
				return 0;
			}

			$data['member_id'] = $info['member_id'];
			$data['pay_amount'] = $info['pay_amount'];
			$data['id'] = $info['id'];
			$data['charge_io'] = 1;
			$data['charge_type'] = 140;
			$data['remark'] = "购买邮箱支付";
		}else{
			//购买短信支付
			$info = Db::name('order_sms')->where('order_sn', $order_sn)->find();
			sys_log("支付订单：".json_encode($info),'buypay.log');
			if($info['status']==20){
				return 1;
			}
			if($info['status']!=10){
				return 0;
			}

			$data['member_id'] = $info['member_id'];
			$data['pay_amount'] = $info['pay_amount'];
			$data['id'] = $info['id'];
			$data['charge_io'] = 1;
			$data['charge_type'] = 130;
			$data['remark'] = "购买短信支付";
		}


		if(!$info){
			return 0;
		}
		//总账户扣款
		sys_log("总账户开始扣款",'buypay.log');
		$rs = $this->outUpdateMainAccount($data);
		sys_log("总账户扣款返回当前余额：".json_encode($rs),'buypay.log');
		if(!$rs['code']){
			return 0;
		}
		//在添加金额之前的账户金额
		$data['current_amount'] = $rs['current_amount'];

		//新增主账户日志
		$log_id = $this->outAddMainAccountLog($data);
		sys_log("新增支付日志返回日志id:".$log_id,'buypay.log');
		if(!$log_id){
			return 0;
		}
		$data['member_account_log_id'] = $log_id;
		//新增子账户,子账户日志在子账户中调用
		$rs = $this->outUpdateSubAccount($data);
		sys_log("子账户扣款返回状态:".$rs,'buypay.log');
		if(!$rs){
			return 0;
		}
		//最终返回主账户日志id
		return 1;
	}
	function outUpdateMainAccount($data){
		$info = Db::name('member_account')->where('member_id', $data['member_id'])->find();
		sys_log("总账户：".json_encode($info),'buypay.log');
		if(!$info){
			sys_log("账户查询失败",'buypay.log');
			return ['code'=>0];
		}
		sys_log("金额对比",'buypay.log');
		if($info['total_amount']<$data['pay_amount']){
			sys_log("账户余额不足",'buypay.log');
			return ['code'=>0];
		}
		
		$current_amount = $info['total_amount']-$data['pay_amount'];
		sys_log($current_amount,'buypay.log');
		sys_log(json_encode($data),'buypay.log');
		sys_log($data['member_id'],'buypay.log');
		$rs = Db::name('member_account')
					->where('member_id', $data['member_id'])
					->update(['total_amount'=>$current_amount]);
		if(!$rs){
			sys_log("扣款失败",'buypay.log');
			return ['code'=>0];
		}

		sys_log("扣款完成后的余额：".$current_amount,'buypay.log');
		return ['code'=>1,'current_amount'=>$current_amount];
	}

	function outAddMainAccountLog($data){
		$upd = [];
		$upd['member_id'] = $data['member_id'];
		$upd['operate_module_id'] = $data['id'];
		$upd['amount'] = $data['pay_amount'];
		$upd['current_amount'] = $data['current_amount'];
		$upd['charge_io'] = $data['charge_io'];
		$upd['charge_type'] = $data['charge_type'];
		$upd['remark'] = $data['remark'];
		$upd['create_time'] = date('Y-m-d H:i:s');
		$id = Db::name('member_account_log')->insertGetId($upd);
		if(!$id){
			return 0;
		}else{
			return $id;
		}
	}

	function outUpdateSubAccount($data){
		$lists = Db::name('member_account_sub')
					->where('member_id', $data['member_id'])
					->where('current_amount','>',0)
					->order('id asc')
					->select();
		sys_log("子账户列表".json_encode($lists),'buypay.log');
		if(!$lists){
			return 0;
		}
		//开始支付
		$pay_amount = $data['pay_amount'];
		foreach ($lists as $k => $v) {
			$current_pay_amount = 0;
			//当前账户的金额小于需要支付的金额
			if($v['current_amount']<$pay_amount){
				$pay_amount -= $v['current_amount'];
				//当前需要支付的金额
				$current_pay_amount = $v['current_amount'];
			}else{
				//当前需要支付的金额
				$current_pay_amount = $pay_amount;
				$pay_amount = 0;
			}

			//子账户扣款
			$rs = Db::name('member_account_sub')
					->where('id', $v['id'])
					->update(['current_amount'=>($v['current_amount']-$current_pay_amount)]);
			if(!$rs){
				return 0;
			}

			sys_log("扣款金额".$current_pay_amount,'buypay.log');
			sys_log("剩余金额".($v['current_amount']-$current_pay_amount),'buypay.log');
			$data['member_account_sub_id'] = $v['id'];
			$data['amount'] = $current_pay_amount;
			$data['current_amount'] = $v['current_amount']-$current_pay_amount;
			//新增子账户日志
			$rs = $this->outAddSubAccountLog($data);
			if(!$rs){
				return 0;
			}
			//不需要在扣款了，跳出
			if($pay_amount==0){
				break;
			}
		}

		sys_log("最终的金额".$pay_amount,'buypay.log');
		if($pay_amount>0){
			return 0;
		}else{
			return 1;
		}
	}

	function outAddSubAccountLog($data){
		$upd = [];
		$upd['member_id'] = $data['member_id'];
		$upd['member_account_sub_id'] = $data['member_account_sub_id'];
		$upd['member_account_log_id'] = $data['member_account_log_id'];
		$upd['operate_module_id'] = $data['id'];
		$upd['amount'] = $data['amount'];
		$upd['current_amount'] = $data['current_amount'];
		$upd['charge_io'] = $data['charge_io'];
		$upd['charge_type'] = $data['charge_type'];
		$upd['remark'] = $data['remark'];
		$upd['create_time'] = date('Y-m-d H:i:s');
		$rs = Db::name('member_account_sub_log')->insert($upd);
		if(!$rs){
			return 0;
		}else{
			return 1;
		}
	}


	//-----------------------------------------退款操作------------------------------------------
	//订单退款
	function refAccountIn($data){
		$amount = $data['pay_amount'];
		$data['amount'] = $amount;
		$data['id'] = $data['id']; //操作的订单ID
		$data['charge_type'] = 135;  //订单退款
		$data['charge_io'] = 0;
		$data['operator'] = '用户';
		$data['remark'] = '退款';
		$data['czje'] = $amount;
		//增加主账户金额
		$total_amount = $this->refUpdateMainAccount($data);
		//在添加金额之前的账户金额
		$data['current_amount'] = $total_amount;
		//新增主账户日志
		$log_id = $this->refAddMainAccountLog($data);
		if(!$log_id){
			return 0;
		}
		$data['member_account_log_id'] = $log_id;
		//子账户操作
		//账户充值
		if($data['pay_amount']>0){
			$data['amount'] = $data['pay_amount'];
			$data['charge_type'] = 135;
			$rs = $this->refAddSubAccountLog($data);
			if(!$rs){
				return 0;
			}
		}
		//最终返回主账户日志id
		return $log_id;
	}
	function refUpdateMainAccount($data){
		$info = Db::name('member_account')
				->where('member_id', $data['member_id'])
				->find();
		if(!$info){
			return 0;
		}

		$total_amount = $data['amount']+$info['total_amount'];
		if(!Db::name('member_account')
				->where('member_id', $data['member_id'])
				->update(['total_amount'=>$total_amount])){
			return 0;
		}
		//返回当前总金额
		return $total_amount;
	}
	function refAddMainAccountLog($data){
		$upd = [];
		$upd['member_id'] = $data['member_id'];
		$upd['operate_module_id'] = $data['id'];
		$upd['amount'] = $data['amount'];
		$upd['current_amount'] = $data['current_amount'];
		$upd['charge_io'] = $data['charge_io'];
		$upd['charge_type'] = $data['charge_type'];
		$upd['operator'] = $data['operator'];
		$upd['remark'] = $data['remark'];
		$upd['create_time'] = date('Y-m-d H:i:s');
		$id = Db::name('member_account_log')->insertGetId($upd);
		if(!$id){
			return 0;
		}else{
			return $id;
		}
	}
	function refAddSubAccountLog($data){
		$list = Db::name('member_account_sub_log')
				->where('operate_module_id', $data['id'])
				->where('charge_type',130)
				->where('charge_io', 1)
				->order('id asc')
				->select();
		if($list){
			foreach($list as $k=>$v){
				$info = [];
				$info = Db::name('member_account_sub')
							->where('id', $v['member_account_sub_id'])
							->find();

				$upd = [];
				$upd['member_id'] = $data['member_id'];
				$upd['member_account_sub_id'] = $v['member_account_sub_id'];
				$upd['member_account_log_id'] = $data['member_account_log_id'];
				$upd['operate_module_id'] = $data['id'];
				$upd['amount'] = $v['amount'];
				$upd['current_amount'] = $info['current_amount'] + $v['amount'];
				$upd['charge_io'] = $data['charge_io'];
				$upd['charge_type'] = $data['charge_type'];
				$upd['remark'] = $data['remark'];
				$upd['create_time'] = date('Y-m-d H:i:s');

				$rs = Db::name('member_account_sub_log')->insert($upd);
				if(!$rs){
					return 0;
				}else{
					//修改子账户金额
					Db::name('member_account_sub')
						->where('id', $info['id'])
						->update(['current_amount'=>$info['current_amount'] + $v['amount']]);
				}
			}
		}

		return 1;
		
	}
}